package com.sora.sora.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.sora.sora.common.model.bo.UserBasicConvert;
import com.sora.sora.common.model.entity.UserEntity;
import com.sora.sora.manager.model.bo.SysUserBasicConvert;
import com.sora.sora.manager.model.vo.UserVO;
import com.sora.sora.manager.mapper.SysUserMapper;
import com.sora.sora.manager.service.SysUserService;
import com.sora.sora.message.service.MailService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    MailService mailService;

    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public int count() {
        return sysUserMapper.count();
    }

    @Override
    public List<UserVO> queryUser(int limit, int page) {
        PageHelper.startPage(page, limit);
        List<UserEntity> entities = sysUserMapper.query();
        return SysUserBasicConvert.INSTANCE.entitiesToVO(entities);
    }

    @Override
    public List<Integer> ban(List<Integer> uids) {
        List<Integer> failure = new ArrayList<>();
        int result;
        for (Integer uid : uids) {
            result = sysUserMapper.banUser(uid);
            if (result <= 0) {
                failure.add(uid);
            }
        }
        return failure;
    }

    @Override
    public boolean ban(int uid, String reason, int time) {
        if (sysUserMapper.queryStatus(uid) == 0) {
            return false;
        }
        if (sysUserMapper.banUser(uid) >= 0) {
            sysUserMapper.ban(uid, reason, new Date(), time);
            return true;
        }
        return false;
    }

    @Override
    public List<Integer> start(List<Integer> uids) {
        List<Integer> failure = new ArrayList<>();
        int result;
        for (Integer uid : uids) {
            result = sysUserMapper.startUser(uid);
            if (result <= 0) {
                failure.add(uid);
            }
        }
        return failure;
    }

    @Override
    public boolean resetUserPassword(int uid) {
        String password = sysUserMapper.queryEmailById(uid);
        int i = sysUserMapper.resetPassword(uid, passwordEncoder.encode(password));
        String email = sysUserMapper.queryEmailById(uid);
        boolean flag = mailService.infoPasswordChange(email);
        return i > 0 || flag;
    }

    @Override
    public boolean resetUserAvatar(int uid) {
        String path = "jiji";
        return sysUserMapper.resetAvatar(uid, path) > 0;
    }

    @Override
    public List<UserVO> queryUserByName(String name, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<UserEntity> userEntities = sysUserMapper.queryUserByName(name + "%");
        return UserBasicConvert.INSTANCE.entitiesConvertToVO(userEntities);
    }

    @Override
    public UserVO queryUserByEmail(String email) {
        UserEntity userEntities = sysUserMapper.queryUserByEmail(email);
        return UserBasicConvert.INSTANCE.entitiesConvertToVO(userEntities);
    }

    @Override
    public UserVO queryUserById(int id) {
        UserEntity userEntity = sysUserMapper.queryUserById(id);
        return UserBasicConvert.INSTANCE.entitiesConvertToVO(userEntity);
    }
}
